﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Xml;
using System.IO;

namespace HasuSLLib
{
    public class DynamicWSCall
    {
        public static void CallWcf()
        {
            string wcfServicUrl = "http://localhost:51851/Service1.svc";
            BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
            IChannelFactory<IRequestChannel> factory = binding.BuildChannelFactory<IRequestChannel>(new BindingParameterCollection());
            factory.Open();

            EndpointAddress address = new EndpointAddress(wcfServicUrl);
            IRequestChannel irc = factory.CreateChannel(address);
            using (irc as IDisposable)
            {
                irc.Open();

                XmlReader reader = XmlReader.Create(new StringReader(
            @"<GetDataUsingDataContract xmlns='http://tempuri.org/'>
<composite xmlns:a='http://schemas.datacontract.org/2004/07/' 
xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
<a:BoolValue>true</a:BoolValue>
<a:StringValue>successfultest</a:StringValue>
</composite>
</GetDataUsingDataContract>"));
                Message m = Message.CreateMessage(MessageVersion.Soap11,
                    "http://tempuri.org/IService1/GetDataUsingDataContract", reader);

                Message ret = irc.Request(m);
                reader.Close();

                Console.WriteLine(ret);
            }

            //close the factory
            factory.Close();
        }
    }
}
